#include "stdio.h"
#include "stdlib.h"
struct stack_array
{
    int head;
    int* array;
};
const int max_stack_len = 10;
bool IsFull(stack_array* s);
bool IsEmpty(stack_array* s);
stack_array* init_stack();
void push(stack_array* s, int element);
int pop(stack_array* s);

//init
stack_array* init_stack()
{
    stack_array* s;
    s = (stack_array*)malloc(sizeof(stack_array));
    if(s == NULL)
    {
        printf("out of space\n");
        return NULL;
    }
    s->array = (int*)malloc(sizeof(int) * max_stack_len);
    if(s->array == NULL)
    {
        free(s);
        printf("out of space\n");
        return NULL;
    }

    s->head = -1;
    return s;
}

void push(stack_array* s, int element)
{
    if(IsFull(s))
    {
        printf("the stack is full, push failed\n");
    }
    else
    {
        s->head++;
        s->array[s->head] = element;
    }
}
int pop(stack_array* s)
{
    int pop_element;
    if(IsEmpty(s))
    {
        printf("the stack is empty, pop failed\n");
        pop_element = 0;
    }
    else
    {
        pop_element = s->array[s->head];
        s->head--;
    }
    return pop_element;
}

bool IsFull(stack_array* s)
{
    if((s->head + 1) >= max_stack_len)
        return true;
    else
        return false;
}

bool IsEmpty(stack_array* s)
{
    if(s->head == -1)
        return true;
    else
        return false;
}

void print_stack(stack_array* s)
{
    int point = s->head;
    while(point>=0)
    {
        printf("%d\n", s->array[point]);
        // don't forget
        point--;
    }
}

int main(int argc, char const *argv[])
{
    stack_array* s;
    int element;
    s = init_stack();
    push(s,3);
    print_stack(s);
    push(s,5);
    push(s,7);
    print_stack(s);
    element = pop(s);
    printf("%d\n", element);
    element = pop(s);
    printf("%d\n", element);
    element = pop(s);
    printf("%d\n", element);
    element = pop(s);
    printf("%d\n", element);
    return 0;
}